package com.hesicare.cgm.worker;

import com.hesicare.cgm.entity.CgmPatient;
import com.hesicare.cgm.entity.CgmPatientDTO;
import com.hesicare.cgm.entity.CgmPatientGlucose;
import com.hesicare.cgm.enums.CgmEnum;
import com.hesicare.cgm.enums.PatientTypeEnum;
import com.hesicare.cgm.service.ICgmBloodGlucoseDayService;
import com.hesicare.cgm.service.ICgmBloodGlucoseService;
import com.hesicare.cgm.service.ICgmPatientService;
import com.hesicare.config.cache.Cache;
import com.hesicare.health.entity.SysPatient;
import com.hesicare.health.service.ISysPatientService;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * @Author 55097
 * @create 2021/12/6 17:31
 */
@Component
public class CgmBloodOnWorker {

    @Autowired
    private Cache cache;

    @Autowired
    private CgmInteractive cgmInteractive;

    @Autowired
    private ISysPatientService ISysPatientService;

    @Autowired
    private ICgmPatientService cgmPatientService;

    @Autowired
    private ICgmBloodGlucoseDayService cgmBloodGlucoseDayService;

    @Autowired
    private ICgmBloodGlucoseService cgmBloodGlucoseService;

    private static String inHospital = " RY";
    private static String outHospital = "CY";
    private static String man = "MALE";
    private static String woman = "FEMALE";
    private static String UNKNOWN = "UNKNOWN";


    protected final static Logger log = LoggerFactory.getLogger(CgmBloodOnWorker.class);

    public boolean getCgmBlood() {
        Set patientNumSet = cache.getSetALL(CgmEnum.PATIENTLIST);
        List<CgmPatientDTO> patientDTOS = new ArrayList<>();
        patientDTOS = ISysPatientService.selectSysPatientBybindingStatus();
        if (patientDTOS.isEmpty()) {
            return true;
        }
        cache.putSetValue(CgmEnum.PATIENTLIST, patientNumSet);
        patientDTOS.stream().forEach(patientDTO -> {
            SysPatient sysPatient = ISysPatientService.selectSysPatientByHospitalizedNum(patientDTO.getNumber());
            saveCgmBlood(patientDTO, sysPatient);
        });
        return true;
    }


    public boolean pushPatient() {
        List<CgmPatient> cgmPatientList = cgmPatientService.selectAllPatient();
        if (cgmPatientList.isEmpty()) {
            return true;
        }
        cgmPatientList.stream().forEach(cgmPatient -> {
            if (PatientTypeEnum.INHOSPITAL.getType().equals(cgmPatient.getStatus())) {
                cgmPatient.setStatus(inHospital);
            } else {
                cgmPatient.setStatus(outHospital);
            }
            if (PatientTypeEnum.MAN.equals(cgmPatient.getSex())) {
                cgmPatient.setSex(man);
            } else if (PatientTypeEnum.WOMAN.equals(cgmPatient.getSex())) {
                cgmPatient.setSex(woman);
            } else {
                cgmPatient.setSex(UNKNOWN);
            }
            cgmInteractive.pushCgmBlood(cgmPatient);
        });
        return true;
    }

    private void saveCgmBlood(CgmPatientDTO cgmPatientDTO, SysPatient sysPatient) {
        try {
            String cgmMessage = cgmInteractive.pullCgmBlood(cgmPatientDTO);
            CgmPatientGlucose cgmPatientGlucose = (CgmPatientGlucose) JSONObject.toBean(JSONObject.fromObject(cgmMessage), CgmPatientGlucose.class);
            cgmBloodGlucoseDayService.insertCgmBloodGlucoseDay(cgmPatientGlucose, sysPatient);
            cgmBloodGlucoseService.insertCgmBloodGlucose(cgmPatientGlucose, sysPatient);
        } catch (Exception e) {
            log.error("读取cgm血糖数据错误{}", cgmPatientDTO.toString());
        }
    }


}
